package com.zoweunion.mechanic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.dao.TSSsWarehouseRegionMapper;
import com.zoweunion.mechanic.entity.TSSsWarehouseRegionEntity;
import com.zoweunion.mechanic.entity.vo.TSSsWarehouseRegionVO;
import com.zoweunion.mechanic.service.ITSSsWarehouseRegionService;
import com.zoweunion.mechanic.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TSSsWarehouseRegionServiceImpl extends ServiceImpl<TSSsWarehouseRegionMapper, TSSsWarehouseRegionEntity> implements ITSSsWarehouseRegionService {

    @Autowired
    private TSSsWarehouseRegionMapper tSSsWarehouseRegionMapper;

    @Override
    public PageInfo listPage(Map<String, Object> map) {

        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
        Integer currentPage = Integer.valueOf(map.get("currentPage").toString());
        PageHelper.startPage(currentPage, pageSize);

        List<TSSsWarehouseRegionVO> mapList = tSSsWarehouseRegionMapper.findByCondition(map);
        PageInfo<TSSsWarehouseRegionVO> mapPageInfo = new PageInfo<>(mapList);
        return mapPageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addWarehouseRegion(List<Map<String, Object>> provinceList, String staffId, String sId, String userId) {
        // 仓库添加管理区域
//        List<AssignRegion> assignRegionList = this.wrapAddAssignRegionList(provinceList, staffId, sId, userId);
        List<TSSsWarehouseRegionEntity> assignRegionList = this.wrapAddWarehouseRegionList(provinceList, staffId, sId, userId);
        if (assignRegionList.size() > 0) {
            tSSsWarehouseRegionMapper.insertWarehouseRegion(assignRegionList);
        }
        return true;
    }

    /**
     * 封装处理，获取将要添加的管理区域列表
     * @param
     * @param sId
     * @return
     */
    private List<TSSsWarehouseRegionEntity> wrapAddWarehouseRegionList(List<Map<String, Object>> provinceList, String staffId, String sId, String userId){
        final String key = "key", title = "title", children = "children";
        List<TSSsWarehouseRegionEntity> assignRegionList = new ArrayList<>();
        if (provinceList != null) {
            for (Map<String, Object> provinceObjMap : provinceList) {
                TSSsWarehouseRegionEntity assignRegionProvince = new TSSsWarehouseRegionEntity();
                assignRegionProvince.setProvinceName(provinceObjMap.get(title).toString());
                assignRegionProvince.setProvinceNumber(provinceObjMap.get(key).toString());
                if (provinceObjMap.get(children) != null) {
                    List<Map<String, Object>> cityList = (List<Map<String, Object>>) provinceObjMap.get(children);
                    if (cityList.size() == 0) {
                        this.wrapSetAssignRegionData(assignRegionProvince, staffId, sId, userId);
                        assignRegionList.add(assignRegionProvince);
                        continue;
                    }
                    for (Map<String, Object> cityObjMap : cityList) {
                        if (cityObjMap.get(key) == null) {
                            continue;
                        }
                        TSSsWarehouseRegionEntity assignRegionCity = new TSSsWarehouseRegionEntity();
                        assignRegionCity.setProvinceName(provinceObjMap.get(title).toString());
                        assignRegionCity.setProvinceNumber(provinceObjMap.get(key).toString());
                        assignRegionCity.setCityName(cityObjMap.get(title).toString());
                        assignRegionCity.setCityNumber(cityObjMap.get(key).toString());
                        if (cityObjMap.get(children) != null) {
                            List<Map<String, Object>> areaList = (List<Map<String, Object>>) cityObjMap.get(children);
                            if (areaList.size() == 0) {
                                this.wrapSetAssignRegionData(assignRegionProvince, staffId, sId, userId);
                                assignRegionList.add(assignRegionCity);
                                continue;
                            }
                            for (Map<String, Object> areaObjMap : areaList) {
                                TSSsWarehouseRegionEntity assignRegionArea = new TSSsWarehouseRegionEntity();
                                assignRegionArea.setProvinceName(provinceObjMap.get(title).toString());
                                assignRegionArea.setProvinceNumber(provinceObjMap.get(key).toString());
                                assignRegionArea.setCityName(cityObjMap.get(title).toString());
                                assignRegionArea.setCityNumber(cityObjMap.get(key).toString());
                                assignRegionArea.setAreaName(areaObjMap.get(title).toString());
                                assignRegionArea.setAreaNumber(areaObjMap.get(key).toString());
                                this.wrapSetAssignRegionData(assignRegionArea, staffId, sId, userId);
                                assignRegionList.add(assignRegionArea);
                            }
                        } else {
                            this.wrapSetAssignRegionData(assignRegionProvince, staffId, sId, userId);
                            assignRegionList.add(assignRegionCity);
                        }
                    }
                } else {
                    this.wrapSetAssignRegionData(assignRegionProvince, staffId, sId, userId);
                    assignRegionList.add(assignRegionProvince);
                }
            }
        }
        return assignRegionList;
    }


    private void wrapSetAssignRegionData(TSSsWarehouseRegionEntity assignRegion, String staffId, String sId, String userId){
        assignRegion.setId(UuidUtil.get32UUID());
        assignRegion.setSId(sId);
        assignRegion.setWarehouseId(staffId);
        assignRegion.setCreateUser(userId);
        assignRegion.setUpdateUser(userId);
    }

//    @Override
//    public List<TSSsWarehouseRegionExcel> listExport(Map<String, Object> params) throws InvocationTargetException, IllegalAccessException {
//
//
//        PageHelper.startPage(1, 10);
//
//        List<TSSsWarehouseRegionVO> tSSsWarehouseRegionVOS = tSSsWarehouseRegionMapper.findByCondition(params);
//
//        // 转化格式
//        List<TSSsWarehouseRegionExcel> excels = new ArrayList<>();
//        TSSsWarehouseRegionExcel excel = new TSSsWarehouseRegionExcel();
//        for (TSSsWarehouseRegionVO item : tSSsWarehouseRegionVOS) {
//            BeanUtils.copyProperties(excel, item);
//            excels.add(excel);
//        }
//
//        return excels;
//    }


}

